package com.quest.space.poker.base.enums;

import com.quest.space.poker.dto.PokerDto;
import com.quest.space.poker.base.utils.PokerUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static com.quest.space.poker.base.bean.PokerConstants.*;


public enum PokerTypeEnum {
    X(1, "单牌", CANONICAL_X),
    XX(2, "对子", CANONICAL_XX),
    XXX(3, "三个", CANONICAL_XXX),
    BOMB(4, "普通炸弹", CANONICAL_XXXX),
    BOMB_KING(5, "王炸", CANONICAL_KING),
    XXX_Y(6, "三带一", CANONICAL_YXXX),
    SHUN(7, "顺子", CANONICAL_ABCDE_N),
    COUPLE(7, "两连对", CANONICAL_AABBCC_N),
    COUPLE_XXX(8, "三连对",CANONICAL_AAABBBCCC_N),
    PLANE(9, "飞机", "");

    private final Integer value;

    private final String desc;

    private final String regular;

    PokerTypeEnum(Integer value, String desc, String regular) {
        this.value = value;
        this.desc = desc;
        this.regular = regular;
    }

    public Integer getValue() {
        return value;
    }

    public String getDesc() {
        return desc;
    }

    public String getRegular() {
        return regular;
    }

    public static PokerTypeEnum getPokerType(String str){
        for (PokerTypeEnum type : PokerTypeEnum.values()) {
            List<String> regulars = new ArrayList<>();
            String regular = type.getRegular();
            if(type.equals(SHUN)){
                if (str.length() >= 5) {
                    regular = regular.replace("N", String.valueOf(str.length() - 1));
                    regulars.add(regular);
                }
            }else if(type.equals(COUPLE)){
                if (str.length() >= 6) {
                    String size = String.valueOf((str.length())/2 -1);
                    regular = regular.replace("N", size);
                    regulars.add(regular);
                }
            }else if(type.equals(COUPLE_XXX)){
                if (str.length() >= 9) {
                    String size = String.valueOf((str.length())/2 -1);
                    regular = regular.replace("N", size);
                    regulars.add(regular);
                }
            } else if (type.equals(PLANE)) {
                PokerTypeEnum plane = matchPlane(str);
                if (plane != null) {
                    return plane;
                }
            } else if (type.equals(XXX_Y)) {
                regulars.add(CANONICAL_YXXX);
                regulars.add(CANONICAL_XXXY);
            }else {
                regulars.add(regular);
            }
            List<Matcher> matchers = initMatcher(regulars, str);
            for (Matcher matcher : matchers) {
                if (matcher.matches()) {
                    return type;
                }
            }
        }
        return null;
    }

    public static List<Matcher> initMatcher(List<String> regulars, String str) {
        return regulars.stream().map(regular -> Pattern.compile(regular).matcher(str)).collect(Collectors.toList());
    }

    private static PokerTypeEnum matchPlane(String str) {
        //可能存在4个一样的，先不考虑
        List<PokerDto> allPoker = PokerUtil.sortPokerStr(str);
        Map<Integer, List<PokerDto>> map = allPoker.stream().collect(Collectors.groupingBy(PokerDto::getValue));
        List<List<PokerDto>> xxx = new ArrayList<>();
        for (Integer v : map.keySet()) {
            List<PokerDto> list = map.get(v);
            if (list.size() == 3) {
                xxx.add(list);
            }
        }
        int single = allPoker.size() - xxx.size() * 3;
        if (xxx.size() >= 2 && single == xxx.size()) {
            return PLANE;
        }
        return null;
    }


    public static void main(String[] args) {
        String str = "3335";
        System.out.println(PokerTypeEnum.getPokerType(str).desc);
        /*//String regular = PokerTypeEnum.SHUN.getRegular();
        String regular = CANONICAL_AAABBBCCC_N;
        //String regular = "(?:3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){4,}\\S";
        //CANONICAL_AABB = "(?:33(?=44)|44(?=55)|55(?=66)|66(?=77)|77(?=88)|88(?=99)){3,}\\S";

        String size = String.valueOf((str.length())/3 -1);
        regular = regular.replace("N", size);

        Matcher matcher = initMatcher(regular, str);
        if (matcher.matches()) {
            System.out.println("匹配了 " + size);
        }else {
            System.out.println("没匹配 " +size);
        }*/
    }


}
